package nl.infodation.emr.pricingservice.controller;

import javax.persistence.EntityManager;

import nl.infodation.emr.pricingservice.entity.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Connector {

    private final static Logger LOG = LoggerFactory.getLogger(Connector.class);
    
    public String getConfig(String name, EntityManager entityManager) {
		Config config = entityManager.find(Config.class, name);
		if (config == null) {
			LOG.warn("No config for \"{}\"", name);
			return null;
		}
		return config.getValue();
    }
    
    public Boolean setConfig(String name, String value, EntityManager entityManager) {
		Config config = entityManager.find(Config.class, name);
		if (config == null) {
			LOG.warn("No config for \"{}\"", name);
			return false;
		}
		entityManager.getTransaction().begin();
		
		config.setValue(value);
		entityManager.merge(config);
		
		entityManager.getTransaction().commit();
		return true;
    }
}